package com.qingke.besuper.app;


import com.qingke.besuper.command.Command;
import com.qingke.besuper.command.CommandFactory;
import com.qingke.besuper.command.CommandFactory.CommandCode;
import com.qingke.besuper.pojo.User;
import com.qingke.besuper.util.BeSuperConsole;

public class BeSuperApp {
	
	private static BeSuperApp instance=null;
	
	private User user=null;
	private boolean isRun=true;
	private BeSuperApp(){
		
	}
	public static BeSuperApp getInstance(){
		if(instance==null){
			instance=new BeSuperApp();
		}
		return instance;
	}
	public void setUser(User user){
		this.user=user;
	}
	public void setIsRun(boolean isRun){
		this.isRun=isRun;
	}
	public void run(){
		BeSuperConsole.println("Welcome to BeSuper.");
		String cmd;
		while(isRun){
			cmd=BeSuperConsole.prompt("cmd>").replaceFirst(" -","_").trim().toUpperCase();
			
			CommandCode cmdCode = null;
			try{
				cmdCode=CommandCode.valueOf(cmd);
			}catch(Exception e){
				BeSuperConsole.println("err input,you can input 'help' to look command.");
				
				continue;
			}
			
			CommandFactory factory =CommandFactory.getFactory(user); 
			Command command = factory.buildCommand(cmdCode);
			if(command!=null)
				command.execute();
			
		}
	}
	
}
